package com.swh.qa.controller;

import java.util.Map;

import com.swh.qa.client.LabelClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.swh.qa.entity.TbProblem;
import com.swh.qa.service.TbProblemService;

import com.swh.base.entity.Result;
import com.swh.base.entity.StatusCode;

/**
 * @author swh
 * @Description 问题 控制器层
 * @date 2021-01-13 19:26:36
 */
@RestController
@CrossOrigin
@Api("tbRroblem")
@RequestMapping("/tbRroblem")
public class TbProblemController {

    @Autowired
    private TbProblemService tbRroblemService;

    @Autowired
    LabelClient labelClient;


    //根据id查询
    @RequestMapping(value = "/label/{labelid}",method = RequestMethod.GET)
    public Result findLabelByLabelId(@PathVariable("labelid") String labelid){

        Result result = labelClient.findById(labelid);
        System.out.println(result);

        return result;
    }

    /**
     * 查询最新问题+分页
     * 需求: 查询问题表中指定标签下的所属问题,按照回复时间倒序排列(有最新回复的在最前面(replytime))
     *
     * @return
     **/
    @RequestMapping(value = "/page/{labelid}/{page}/{size}", method = RequestMethod.GET)
    public Result page(@PathVariable("labelid") String labelid, @PathVariable("page")
            int page, @PathVariable("size") int size) {
        Page<Map> page1 = tbRroblemService.page(labelid, page, size);
        return new Result(StatusCode.OK, "查询成功", page1);
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, "查询成功", tbRroblemService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, "查询成功", tbRroblemService.findById(id));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<TbProblem> pageList = tbRroblemService.findSearch(searchMap, page, size);
        return new Result(StatusCode.OK, "查询成功", pageList);
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap) {
        return new Result(StatusCode.OK, "查询成功", tbRroblemService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param tbRroblem
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(TbProblem tbRroblem) {
        tbRroblemService.add(tbRroblem);
        return new Result(StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param tbRroblem
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(TbProblem tbRroblem, @PathVariable String id) {
        tbRroblem.setId(id);
        tbRroblemService.update(tbRroblem);
        return new Result(StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        tbRroblemService.deleteById(id);
        return new Result(StatusCode.OK, "删除成功");
    }

}
